﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DAH_TD_
{
    public class HUD
    {
        Rectangle mouseCul;
        Texture2D texture;
        Texture2D mouseSprite;
        Texture2D temp;
        Texture2D life;
            Rectangle lifePos = new Rectangle(680, 405, 50, 50);
        Texture2D power;
            Rectangle powerPos = new Rectangle(550, 405, 50, 50);
        Texture2D back;
            Rectangle backRect = new Rectangle(0, 400, 800, 80);

        TimeSpan totalGt = new TimeSpan();
        TimeSpan gtTemp = new TimeSpan();
        TimeSpan displayDuration = new TimeSpan();

        List<HUDelement> hudelements;

        TileMap map;

        String infoText = "";
        Vector2 infoTextPos= new Vector2(300, 415);
            bool displayInfoText = false;
        bool dragging = false;
        int lastType;

        public HUD (TileMap map, ContentManager content, Level l)
        {
            mouseCul = new Rectangle(0, 0, 30, 30);

            mouseSprite = content.Load<Texture2D>("Graphics/MouseSprite1");
            temp = mouseSprite;
            back = content.Load<Texture2D>("Graphics/blackpx");
            life = content.Load<Texture2D>("Graphics/Life");
            power = content.Load<Texture2D>("Graphics/Power");

            hudelements = new List<HUDelement>();
            int x = 20;
                for (int i = 0; i < l.getPossibleTowers().Count; i++) {
                    texture = content.Load<Texture2D>(l.getPossibleTowers()[i].texturePath);
                    hudelements.Add(new HUDelement(texture, new Rectangle(x, 410, 50, 50), false, l.getPossibleTowers()[i].getType()));
                    x = x + 70;
                }
            
            this.map = map;            
        }

        public void Update(Level l, Game1 g, GameTime gt)
        {
            // Aktualisierung des Mausrechtecks
            this.mouseCul.X = Mouse.GetState().X;
            this.mouseCul.Y = Mouse.GetState().Y;
                        
            // Maussteurung der Hud-Elemente
            for (int i = 0; i < hudelements.Count; i++) { 
                // Turm ist kaufbar ? -> Textur aufhellen
                if (l.findTowerByType(hudelements[i].type).getCosts() <= l.getResources()){
                    hudelements[i].color = Color.White;
                }
                else { hudelements[i].color = Color.Black; }
                //  Mausklick auf HUD-Element
                if (hudelements[i].rectangle.Intersects(mouseCul) && Mouse.GetState().LeftButton == ButtonState.Pressed && l.findTowerByType(hudelements[i].type).getCosts() <= l.getResources() && !dragging){
                    mouseSprite = hudelements[i].texture;
                    mouseCul.Height = hudelements[i].rectangle.Height;
                    mouseCul.Width = hudelements[i].rectangle.Width;
                    dragging = true;
                    lastType = hudelements[i].type;   
                }
            }
            // Loslassen eines Elements
            if (dragging){
                // Setzen eines Turms
                if (Mouse.GetState().LeftButton == ButtonState.Released)
                {
                    mouseSprite = temp;
                    mouseCul.Width = 30; mouseCul.Height = 30;
                    // Einrasten des Towers und setzen vvvv
                    if (!mouseCul.Intersects(backRect) && mouseCul.Intersects(new Rectangle(0, 0, map.GetWidth() * map.getTileWidth(), map.GetHeight() * map.getTileHeight())) && mouseCul.X < map.getTileWidth() * map.GetWidth() && mouseCul.Y < map.getTileHeight() * map.GetHeight())
                    {
                        Vector2 mousePosCorrected = new Vector2(mouseCul.X + (map.getTileWidth() / 2 - 1), mouseCul.Y + (map.getTileHeight() / 2 - 1));
                        // Wenn kein WEg und freies Feld
                        if (map.GetTile((int)map.PosToTile(mousePosCorrected).X, (int)map.PosToTile(mousePosCorrected).Y).weg == false && map.GetTile((int)map.PosToTile(mousePosCorrected).X, (int)map.PosToTile(mousePosCorrected).Y).occupied == false)
                        {

                            g.AddTower(lastType, mousePosCorrected); // tower setzen
                            l.setResources(l.getResources() - l.findTowerByType(lastType).getCosts()); // ressourcen setzen
                            map.GetTile((int)map.PosToTile(mousePosCorrected).X, (int)map.PosToTile(mousePosCorrected).Y).occupied = true; // tile "besetzt" melden
                            dragging = false;
                        }
                        else
                        {
                            showInfoText("Das kann hier nicht platziert werden", new Vector2(mouseCul.X, mouseCul.Y), 1, gt);
                            dragging = false;
                        }
                    }
                    dragging = false;
                }
            }

            // Verschwinden des Info-Textes
            totalGt = TimeSpan.FromSeconds(gt.TotalGameTime.TotalSeconds);
            if (gtTemp + displayDuration < totalGt) { displayInfoText = false; }
        }

        protected void UnloadContent()
        {
        }

    
        public void Draw(SpriteBatch spriteBatch, SpriteFont font1, Level l)
        {
            
            spriteBatch.Draw(back, backRect, Color.White);
            spriteBatch.Draw(life, lifePos, Color.White);
            spriteBatch.Draw(power, powerPos, Color.White);
            for (int i = 0; i < hudelements.Count; i++) {
                spriteBatch.Draw(hudelements[i].texture, hudelements[i].rectangle, hudelements[i].color);
                spriteBatch.DrawString(font1, Convert.ToString(l.getPossibleTowers()[i].getCosts()), new Vector2(hudelements[i].rectangle.X, hudelements[i].rectangle.Y),Color.White); 
            }

            if (displayInfoText){
                spriteBatch.DrawString(font1, infoText, infoTextPos, Color.Red);
            }

            spriteBatch.Draw(mouseSprite, mouseCul, Color.White);
            spriteBatch.DrawString(font1, totalGt.ToString(), new Vector2(0, 0), Color.White);
        }

        // InfoText anzeigen
        public void showInfoText(String text, Vector2 pos, double displaytime, GameTime gt){
            displayInfoText = true;
            infoText = text;
            infoTextPos = pos;
            displayDuration = TimeSpan.FromSeconds(displaytime);
            gtTemp = TimeSpan.FromSeconds(gt.TotalGameTime.TotalSeconds);
        }

    }
}
